package com.bike.bikeserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bike.bikeserver.common.core.domain.CommonResult;
import com.bike.bikeserver.common.utils.StringUtils;
import com.bike.bikeserver.domain.FeedbackSaying;
import com.bike.bikeserver.domain.vo.ReplayFeedBackVo;
import com.bike.bikeserver.mapper.FeedbackSayingMapper;
import com.bike.bikeserver.service.IFeedbackSayingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/8/2 16:58
 */
@Service("feedbackSayingService")
public class FeedbackSayingServiceImpl implements IFeedbackSayingService
{
    @Autowired
    FeedbackSayingMapper feedbackSayingMapper;

    @Override
    public CommonResult saveFeedbackSaying(FeedbackSaying feedbackSaying)
    {
        try
        {
            feedbackSayingMapper.insert(feedbackSaying);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return CommonResult.success();
    }

    @Override
    public List<FeedbackSaying> getUnFeedBackSayingByNickName(String nickName)
    {
        LambdaQueryWrapper<FeedbackSaying> feedbackSayingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feedbackSayingLambdaQueryWrapper.eq(FeedbackSaying::getAsker, nickName);
        feedbackSayingLambdaQueryWrapper.ne(FeedbackSaying::getFanswer, "待回复");
        return feedbackSayingMapper.selectList(feedbackSayingLambdaQueryWrapper);
    }

    @Override
    public CommonResult getAllFeedBackSaying()
    {
        List<FeedbackSaying> feedbackSayings = feedbackSayingMapper.selectList(null);
        return CommonResult.success(feedbackSayings);
    }

    @Override
    public CommonResult replyFeedbackSaying(ReplayFeedBackVo replayFeedBackVo)
    {
        if (replayFeedBackVo.getReplyText() == null || StringUtils.isBlank(replayFeedBackVo.getReplyText()))
        {
            return CommonResult.warn("不能什么也不回复！");
        }
        FeedbackSaying saying = feedbackSayingMapper.selectById(replayFeedBackVo.getSayingId());
        saying.setFanswer(replayFeedBackVo.getReplyText());
        feedbackSayingMapper.updateById(saying);
        return CommonResult.success();
    }

    @Override
    public CommonResult getUnFeedBackSaying(String replyText)
    {
        LambdaQueryWrapper<FeedbackSaying> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(FeedbackSaying::getFanswer, replyText);
        List<FeedbackSaying> feedbackSayings = feedbackSayingMapper.selectList(wrapper);
        return CommonResult.success("查询成功！为您找到" + feedbackSayings.size() + "条记录！", feedbackSayings);
    }
}
